import { createStore, applyMiddleware } from 'redux'
import list from '../mock/mock'
/* 异步action方法 */
import Thunk from 'redux-thunk'
/* 收藏action */
function collect_action(item) {
    return {
        type: 'COLLECT',
        item
    }
}
/* 取消收藏action */
function qxcollect_action(item) {
    return {
        type: 'QXCOLLECT',
        item
    }
}
/* 添加购物车 */
function addgwc_action(item) {
    return {
        type: 'ADDGWC',
        item
    }
}
/* 购物车商品数量+ */
function add_action(item) {
    return {
        type: 'ADD',
        item
    }
}
/* 购物车商品数量- */
function rem_action(item) {
    return {
        type: 'REM',
        item
    }
}
/* 清空购物车 */
function qk_action() {
    return {
        type: 'QK',
    }
}

/* 新增地址 */
function adddz_action(item) {
    return {
        type: 'ADDDZ',
        item
    }
}
/* 删除地址 */
function scdz_action(item) {
    return {
        type: 'SCDZ',
        item
    }
}
/* 编辑地址 */
function bjdz_action(item) {
    return {
        type: 'BJDZ',
        item
    }
}
/* 搜索数据 */
function filter_action(item) {
    return {
        type: 'FILTER',
        item
    }
}
/* 搜索数据历史记录 */
function lsjl_action(item) {
    return {
        type: 'LSJL',
        item
    }
}
/* 保存默认地址 */
function dzaddress_action(item) {
    return {
        type: 'DZADDRESS',
        item
    }
}
/* 点击结算添加到订单页 */
function qjssp_action(item) {
    return {
        type: 'QJSSP',
        item
    }
}

let data = {
    list: list.data.list,
    lists: list.data.list,
    sclist: localStorage.getItem('sclist') ? JSON.parse(localStorage.getItem('sclist')) : [],//收藏店铺列表
    gwclist: localStorage.getItem('gwclist') ? JSON.parse(localStorage.getItem('gwclist')) : [],//购物车列表
    adddz: localStorage.getItem('adddz') ? JSON.parse(localStorage.getItem('adddz')) : [],//新增地址列表
    sslist: [],//搜索列表
    lsjl: localStorage.getItem('lsjl') ? JSON.parse(localStorage.getItem('lsjl')) : [],//搜索历史
    dzaddress: localStorage.getItem('dzaddress') ? JSON.parse(localStorage.getItem('dzaddress')) : {}, //保存默认地址
    splist: localStorage.getItem('splist') ? JSON.parse(localStorage.getItem('splist')) : []
}

function reducer(state = data, action) {
    /* 深拷贝 */
    state = JSON.parse(JSON.stringify(state))
    switch (action.type) {
        /* 当触发收藏时添加数据到收藏列表 */
        case 'COLLECT':
            var finditem = state.sclist.findIndex(item => {
                return item.id === action.item.id
            })
            /* 判断是否已添加收藏 */
            if (finditem === -1) {
                /* 收藏列表中没有才添加 */
                state.sclist.push(action.item)
            }
            /* 持久化数据 */
            localStorage.setItem('sclist', JSON.stringify(state.sclist))
            return state

        /* 当触发取消收藏时取消当前数据 */
        case 'QXCOLLECT':
            var findi = state.sclist.findIndex(item => {
                return item.id === action.item.id
            })
            /* 收藏列表中找到对应数据删除 */
            if (findi !== -1) {
                state.sclist.splice(findi, 1)
            }
            /* 持久化数据 */
            localStorage.setItem('sclist', JSON.stringify(state.sclist))
            return state

        /* 当触发添加购物车添加数据到列表 */
        case 'ADDGWC':
            state.gwclist.push(action.item)
            return state

        /* 购物车数量+ */
        case 'ADD':
            /* 购物车列表中找到对应数据 */
            let findit = state.gwclist.find(item => {
                return item.id === action.item.id
            })
            /* 找到增加数量 */
            if (findit) {
                findit.count++
            }
            /* 持久化数据 */
            localStorage.setItem('gwclist', JSON.stringify(state.gwclist))
            return state

        /* 购物车数量- */
        case 'REM':
            /* 购物车列表中找到对应数据 */
            let findite = state.gwclist.find(item => {
                return item.id === action.item.id
            })
            /* 找到并且数量>1时执行-1 */
            if (findite && findite.count > 1) {
                findite.count--
            } else {
                /* 否则 */
                let findin = state.gwclist.findIndex(item => {
                    return item.id === action.item.id
                })
                /* 找到对应项删除 */
                if (findin !== -1) {
                    state.gwclist.splice(findin, 1)
                }
            }
            /* 持久化数据 */
            localStorage.setItem('gwclist', JSON.stringify(state.gwclist))
            return state

        /* 清空购物车 */
        case 'QK':
            state.gwclist.splice(0, state.gwclist.length)
            return state

        /* 新增地址 */
        case 'ADDDZ':
            /* 给每个item一个id */
            action.item.id = new Date() * 1
            state.adddz.push(action.item)
            /* 持久化数据 */
            localStorage.setItem('adddz', JSON.stringify(state.adddz))
            return state

        /* 删除地址 */
        case 'SCDZ':
            /* 地址列表中找到对应数据 */
            let findid = state.adddz.findIndex(item => {
                return item.id === action.item.id
            })
            /* 找到对应项删除 */
            if (findid !== -1) {
                state.adddz.splice(findid, 1)
                /* 并且在是默认地址时删除默认找到 */
                if (findid === state.dzaddress.id) {
                    state.dzaddress = {}
                }
            }
            /* 持久化数据 */
            localStorage.setItem('adddz', JSON.stringify(state.adddz))
            return state

        /* 编辑地址 */
        case 'BJDZ':
            /* 地址列表中找到对应数据 */
            let findbj = state.adddz.findIndex(item => {
                return item.id === action.item.id
            })
            /* 找到对应数据替换数据 */
            if (findbj !== -1) {
                state.adddz.splice(findbj, 1, action.item)
            }
            /* 持久化数据 */
            localStorage.setItem('adddz', JSON.stringify(state.adddz))
            return state

        /* 保存默认地址 */
        case 'DZADDRESS':
            state.dzaddress = action.item
            localStorage.setItem('state.dzaddress', JSON.stringify(state.dzaddress))
            return state

        /* 搜索店铺 */
        case 'FILTER':
            state.list = state.lists
            let slist = state.list.filter(i => {
                return i.title.includes(action.item)
            })
            if (slist.length > 0) {
                state.list = slist
            }
            return state

        /* 搜索店铺历史记录 */
        case 'LSJL':
            if (action.item && action.item !== ' ') {
                state.lsjl.push(action.item)
            }
            localStorage.setItem('lsjl', JSON.stringify(state.lsjl))
            return state
        /* 结算 */
        case 'QJSSP':
            state.splist.push(action.item)
            console.log(state.splist);
            localStorage.setItem('splist', JSON.stringify(state.splist))
            return state
        default:
            return state
    }
}

let store = createStore(reducer, applyMiddleware(Thunk))

export {
    store,
    collect_action,
    qxcollect_action,
    addgwc_action,
    add_action,
    rem_action,
    qk_action,
    adddz_action,
    scdz_action,
    filter_action,
    lsjl_action,
    bjdz_action,
    dzaddress_action,
    qjssp_action
}